package com.apes.scm.masterdata.product.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.masterdata.product.model.ServeCategory;
import com.apes.scm.masterdata.product.repository.ServeCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 *
 * @Auther: linwz
 * @Date: 2018/8/10 16:52
 */
@Service("serveCategoryService")
public class ServeCategoryService extends DomainService {
    @Autowired
    ServeCategoryRepository serveCategoryRepository;

    public ServeCategory save(SimpleRequest request) {
        ServeCategory serveCategory = request.getO(ServeCategory.class);
        checkUnique(serveCategory);
        return serveCategoryRepository.saveAndFlush(serveCategory);
    }

    /**
     * 功能：查询
     *
     * @param request
     */
    public Page<ServeCategory> findAll(SimpleRequest request) {
        List<ServeCategory> list = request.get("condition");
        return serveCategoryRepository.findAll(JpaDsl.toCriteria(list) ,PageRequest.of(request.get("page"),request.get("size")));
    }

    /**
     * 删除
     * @param request
     * @return
     * @throws Exception
     */
    public String delete(SimpleRequest request) throws Exception  {

        ServeCategory oldServeCategory = serveCategoryRepository.findOne((String) request.get("id"));
        if (oldServeCategory == null) throw new RuntimeException("当前定义在系统中不存在。");
        if(oldServeCategory.getVersion() != (int) request.get("version")) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改

        ServeCategory serveCategory = request.getO(ServeCategory.class);

        serveCategoryRepository.delete(serveCategory);
        return "删除成功";
    }

    /***
     * 通过商品分类获取服务类型
     */
    public ServeCategory findByClassify(Classify classify) {
        return serveCategoryRepository.findAll(JpaDsl.toCriteriaByEq(
                "classify", classify)).stream().findFirst().orElse(null);
    }


    /***
     * 限制一个商品分类只允许有一个服务类型
     */
    private void checkUnique(ServeCategory serveCategory) {

        ServeCategory tempServeCategory = serveCategory.getId() == null ?
                serveCategoryRepository.findOne(JpaDsl.toCriteriaByEq(
                        "classify.id", serveCategory.getClassify().getId(),
                        "serveType.id", serveCategory.getServeType().getId()
                )).orElse(null) :
                serveCategoryRepository.findOne(JpaDsl.toCriteria(
                        "classify.id", "EQ", serveCategory.getClassify().getId(),
                        "serveType.id", "EQ", serveCategory.getServeType().getId(),
                        "id", "NE", serveCategory.getId()

                )).orElse(null);
        if(tempServeCategory != null) throw new RuntimeException("当前商品分类存在重复的服务类型，分类名称：" + serveCategory.getClassify().getName());
    }

}
